package com.android.clinic.app.ui.personal;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.android.clinic.app.R;
import com.android.clinic.app.base.BaseActivity;
import com.android.clinic.app.http.HttpClient;
import com.android.clinic.app.http.base.BaseObserver;
import com.android.clinic.app.http.utils.RxObservableUtils;
import com.android.clinic.app.utils.Utils;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 忘记密码
 * Created by cheng on 2018/9/18.
 */
public class FindPasswordActivity extends BaseActivity {

    private EditText etMobile, etCode, etPassword, etPassword2;
    private Button btnSend;

    private String mMobile;
    private TimeCount mTimeCount;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_find_passqord;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        setTitleBarTitle("忘记密码");
        setTitleBarLeftResource(R.drawable.icon_back);
        mTimeCount = new TimeCount(60000, 1000);

        etMobile = (EditText) findViewById(R.id.et_mobile);
        etCode = (EditText) findViewById(R.id.et_code);
        etPassword = (EditText) findViewById(R.id.et_password);
        etPassword2 = (EditText) findViewById(R.id.et_password2);
        btnSend = (Button) findViewById(R.id.btn_send_code);
    }

    @Override
    protected void widgetClick(View v) {
        super.widgetClick(v);
        switch (v.getId()) {
            case R.id.btn_send_code:
                if (btnSend.getText().toString().equals("重新验证")) {
                    etCode.setText("");
                }
                getVerifyCode();
                break;
            case R.id.btn_confirm:
                resetPassword();
                break;
        }
    }

    /**
     * 获取验证码
     */
    private void getVerifyCode() {
        mMobile = etMobile.getText().toString().trim();
        if (Utils.isEmpty(mMobile)) {
            showToast("手机号不能为空!");
            return;
        } else if (!Utils.matchPhone(mMobile)) {
            showToast("请输入正确的手机号!");
            return;
        }
        HttpClient.getHttpService().resetSendCodeByMobile(mMobile)
                .compose(this.bindToLifecycle())
                .compose(RxObservableUtils.applySchedulers())
                .subscribe(new BaseObserver<String>() {
                    @Override
                    public void onSuccess(String s) {
                        showToast("验证信息已发送您手机!");
                        mTimeCount.start();
                    }

                    @Override
                    public void onFailure(String message) {
                        showToast(message);
                    }
                });

    }

    /**
     * 重置密码
     */
    private void resetPassword() {
        String code = etCode.getText().toString().trim();
        String password = etPassword.getText().toString().trim();
        String password2 = etPassword2.getText().toString().trim();
        if (Utils.isEmpty(code)) {
            showToast("请输入手机收到的验证码!");
            return;
        } else if (Utils.isEmpty(password)) {
            showToast("密码不能为空!");
            return;
        } else if (Utils.isEmpty(password2)) {
            showToast("确认密码不能为空!");
            return;
        } else if (!password.equals(password2)) {
            showToast("两次密码输入一致!");
            return;
        }
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", code);
            jsonObject.put("mobile", mMobile);
            jsonObject.put("password", password);

            MediaType jsonType = MediaType.parse("application/json; charset=utf-8");
            RequestBody body = RequestBody.create(jsonType, jsonObject.toString());

            HttpClient.getHttpService().resetPassword(body)
                    .compose(this.bindToLifecycle())
                    .compose(RxObservableUtils.applySchedulers(loading, "正在提交..."))
                    .subscribe(new BaseObserver<String>() {
                        @Override
                        public void onSuccess(String s) {
                            showToast("密码已修改成功，请从新登录!");
                            finish();
                        }

                        @Override
                        public void onFailure(String message) {
                            showToast(message);
                        }
                    });

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /* 定义一个倒计时的内部类 */
    private class TimeCount extends CountDownTimer {
        private TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
        }

        @Override
        public void onFinish() {// 计时完毕时触发
            btnSend.setText("重新验证");
            btnSend.setClickable(true);
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程显示
            btnSend.setClickable(false);
            btnSend.setText(millisUntilFinished / 1000 + "秒");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != mTimeCount){
            mTimeCount.cancel();
        }
    }
}
